#import "CTEnvironmentManager.h"
#import "BaseNetRequest.h"
#import <MJExtension/MJExtension.h>
@interface CTEnvironmentManager ()
@property (nonatomic,strong,readwrite) CTEnvironment * environmentSetting;
@property (nonatomic ,copy) NSString *baseDomain;
@property (nonatomic, copy, readwrite) NSString *certificateServicesUrl;
@property (nonatomic, copy, readwrite) NSString *privacyPolicyUrl;
@property (nonatomic, copy, readwrite) NSString *userAgreementUrl;
@property (nonatomic, copy, readwrite) NSString *logBaseUrl;
@property (nonatomic, copy, readwrite) NSString *translateGateWay;
@property (nonatomic, copy, readwrite) NSString *baseUrlPath;
@property (nonatomic, copy, readwrite) NSString *imagePath;
@property (nonatomic, copy, readwrite) NSString *reportPath;
@property (nonatomic, copy, readwrite) NSString *normalPath;
@property (nonatomic ,copy, readwrite) NSString *creditPath;
@property (nonatomic, copy, readwrite) NSString *business;
@property (nonatomic, copy, readwrite) NSString *channel;
@property (nonatomic, copy, readwrite) NSString *district;
@property (nonatomic, copy, readwrite) NSString *chBiz;
@property (nonatomic, copy, readwrite) NSString *chSub;
@property (nonatomic, copy, readwrite) NSString *appid;
@property (nonatomic, copy, readwrite) NSString *loginFromType;
@end
@implementation CTEnvironmentManager
+ (instancetype)shareManager
{
    static CTEnvironmentManager *instance ;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        instance = [[super allocWithZone:NULL] init];
        [instance singletonInitlize];
    });
    return instance;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
    return [CTEnvironmentManager shareManager];
}
- (id)copyWithZone:(struct _NSZone *)zone {
    return [CTEnvironmentManager shareManager];
}
- (void)singletonInitlize
{
    self.areaType = [self.environmentSetting.appEnvironmentAreaType unsignedIntegerValue];
    self.environmentType = [self.environmentSetting.appEnvironmentProductType unsignedIntegerValue];
}
- (void)setEnvironmentType:(AppEnvironmentType)environmentType
{
    _environmentType = environmentType;
    NSArray * environmentArray = self.environmentSetting.environments;
    if (self.environmentType > (environmentArray.count-1)) {
        _environmentType = AppEnvironmentTypeTest;
    }
    CTEnvironmentItem * item = environmentArray[environmentType];
    CTEnvironmentBaseUrls * currentUrl = item.baseUrls;
    self.baseUrlPath = currentUrl.baseUrlPath;
    self.baseUrlPath = currentUrl.baseUrlPath;
    self.normalPath = currentUrl.normalPath;
    self.imagePath = currentUrl.imagePath;
    self.reportPath = currentUrl.reportPath;
    self.creditPath = currentUrl.creditPath;
    self.environmentH5Url = currentUrl.h5url;
    self.logBaseUrl = currentUrl.logBaseUrl;
    self.translateGateWay = currentUrl.translateGateWay;
    self.certificateServicesUrl = currentUrl.certificateServicesUrl;
    self.privacyPolicyUrl = currentUrl.privacyPolicyUrl;
    self.userAgreementUrl = currentUrl.userAgreementUrl;
    self.business = currentUrl.business;
    self.channel = currentUrl.channel;
    self.chBiz = currentUrl.chBiz;
    self.chSub = currentUrl.chSub;
    self.district = currentUrl.district;
    self.appid = currentUrl.appid;
    self.loginFromType = currentUrl.loginFromType;
}
#pragma mark - lazy load
- (CTEnvironment *)environmentSetting
{
    if (_environmentSetting == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"CTEnvironment" ofType:@"plist"];
        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
        _environmentSetting = [CTEnvironment mj_objectWithKeyValues:dict];
    }
    return _environmentSetting;
}
- (NSString *)environmentUrl {
    if (self.environmentType == 1) {
        _environmentUrl = [NSString stringWithFormat:@"https://chint.%@/", [self getBaseDomain]];
        return _environmentUrl;
    } else {
        return @"";
    }
}
- (NSString *)environmentH5Url {
    if (self.environmentType == 1) {
        _environmentH5Url = [NSString stringWithFormat:@"https://m.%@/index.html", [self getBaseDomain]];
        return _environmentH5Url;
    } else {
        return @"";
    }
}
- (NSString *)getBaseDomain {
    NSString *domain = [[NSUserDefaults standardUserDefaults] objectForKey:@"kDomain"];
    if (!beNil(domain)) {
        return domain;
    } else {
        return @"fastbucksng.com";
    }
}
@end
